package com.adwhirl.adapters;

import android.util.Log;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.obj.Ration;
import com.adwhirl.util.AdWhirlUtil;
import com.greystripe.android.sdk.*;

public final class GreystripeAdapter extends AdWhirlAdapter implements BannerListener {
	
	private static GSSDK sdk;
	
	public GreystripeAdapter(AdWhirlLayout adWhirlLayout, Ration ration) {
		super(adWhirlLayout, ration);
		
		if (sdk == null) {
			GSSDK.initialize(this.adWhirlLayout.activity, ration.key);
		}
	}

	@Override
	public void handle() {
		BannerView banner = new BannerView(this.adWhirlLayout.activity);
		banner.addListener(this);
		
		banner.refresh();
	}

	@Override
	public void onFailedToReceiveAd(BannerView view) {
		Log.d(AdWhirlUtil.ADWHIRL, "Greystripe banner failed");
		adWhirlLayout.rollover();
	}

	@Override
	public void onReceivedAd(BannerView view) {
		Log.d(AdWhirlUtil.ADWHIRL, "Greystripe banner successfully returned");
		adWhirlLayout.adWhirlManager.resetRollover();
		adWhirlLayout.nextView = view;
		adWhirlLayout.handler.post(adWhirlLayout.viewRunnable);
		adWhirlLayout.rotateThreadedDelayed();
	}
}
